#__Author:Administrator
#__Date:2020/4/22
from mng import models
from django.http import HttpResponse

def test(request):
    # objs = [
    #   models.Boy(name='summer'),
    #   models.Boy(name='spring'),
    #   models.Boy(name='autumn'),
    # ]
    # models.Boy.objects.bulk_create(objs,5)

    # objs = [
    #   models.Girl(name='小于'),
    #   models.Girl(name='小秋'),
    #   models.Girl(name='小夏'),
    # ]
    # models.Girl.objects.bulk_create(objs,5)

    # 查询和girl小于有关系的boy

    # 1 通过girl表进行反向查找
    obj = models.Girl.objects.filter(name='小于').first().love_set.all()
          models.Teachers.object.filter(name="lili").first().teacherclass_set.all()
    # obj = models.Girl.objects.filter(name='小于').first().love_set.select_related('b').all()
    for i in obj:
        print(i.b.name)

    print(' '.center(60, '='))

    # 2 通过Love表进行查找

    obj = models.Love.objects.filter(g__name='小于').all()
    for i in obj:
        print(i.b.name)

    # 这样进行查询性能不好，需要重新发sql请求在进行查询另外一张表中的数据

    boy_list = models.Love.objects.filter(g__name='小于').values('b__name')
    for i in boy_list:
        print(i['b__name'])

    # 这样 进行了 优化 不会重复发送sql请求，查询到的结果内是字典

    boy_obj = models.Love.objects.filter(g__name='小于').select_related('b').all()
    for i in boy_obj:
        print(i.b.name)
    # select_related('ut') 相当 于 inner join 先连成一张表再进行查询

    return HttpResponse('insert ok ....')


